package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminTopicService;
import com.cskaoyan.service.AdminTopicServiceimpl;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.GoodsServiceimpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.StringUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author Ramsey
 * @date 2023年05月31日 21:21
 */
@WebServlet("/admin/topic/*")
public class AdminTopicServlet extends WdBaseServlet {
    AdminTopicService topicService = new AdminTopicServiceimpl();
    GoodsService goodsService = new GoodsServiceimpl();


    public BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        Integer page = StringUtil.isNotEmpty(request.getParameter("page")) ? Integer.parseInt(request.getParameter("page")) : null;
        Integer limit = StringUtil.isNotEmpty(request.getParameter("limit")) ? Integer.parseInt(request.getParameter("limit")) : null;
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");

        String title = request.getParameter("title");
        String subtitle = request.getParameter("subtitle");

        CommonData<MarketTopic> data = topicService.querrySelective(title, subtitle, page, limit, sort, order);

        return BaseRespVo.ok(data);
    }

    @SneakyThrows
    public BaseRespVo create(HttpServletRequest request, HttpServletResponse response) {
        String jsonStr = request.getReader().readLine();
        MarketTopic marketTopic = new MarketTopic();
        marketTopic = JacksonUtil.read(jsonStr, MarketTopic.class);
        System.out.println(marketTopic);//这里要打断点看
        marketTopic.setId(null);//这里set失败 可能marketTopic==null
        java.util.Date updateTime = new Date();//获得当前时间
        marketTopic.setAddTime(updateTime);//新增时间和更新时间一样
        marketTopic.setUpdateTime(updateTime);
        topicService.add(marketTopic);
        return BaseRespVo.ok(marketTopic);
    }

    @SneakyThrows
    public BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {
        String jsonStr = request.getReader().readLine();
        MarketTopic marketTopic = JacksonUtil.read(jsonStr, MarketTopic.class);
        marketTopic.setDeleted(true);
        java.util.Date updateTime = new Date();//获得当前时间
        marketTopic.setUpdateTime(updateTime);
        topicService.update(marketTopic);
        return BaseRespVo.ok(marketTopic);
    }


    @SneakyThrows
    public BaseRespVo update(HttpServletRequest request, HttpServletResponse response) {
        String jsonStr = request.getReader().readLine();
        MarketTopic marketTopic = JacksonUtil.read(jsonStr, MarketTopic.class);
        System.out.println(marketTopic);//这里要打断点看
        java.util.Date updateTime = new Date();//获得当前时间
        marketTopic.setUpdateTime(updateTime);
        topicService.update(marketTopic);
        return BaseRespVo.ok(marketTopic);

    }


    @SneakyThrows
    public BaseRespVo read(HttpServletRequest request, HttpServletResponse response) {

        Integer id = StringUtil.isNotEmpty(request.getParameter("id")) ? Integer.parseInt(request.getParameter("id")) : null;
        //尝试用老师的map添加
        Map<String, Object> data = new HashMap<>();

        MarketTopic marketTopic = topicService.selectOne(id);
        //看看有没有货物
        Integer[] goods = marketTopic.getGoods();//这个是如果有货物里面装的是货物的id

        //找一个list装起来，在用map把list放进去
        List<MarketGoods> goodsList = new ArrayList<>();

        if (goods == null || goods.length == 0) {
            //这种就是没有
            data.put("goodsList", goods);
        } else {
            for (int i = 0; i < goods.length; i++) {
                Integer goodsId = goods[0];//找到id
                MarketGoods marketGoods = goodsService.selectOne(goodsId);//根据唯一的获取id获取唯一的一个对象
                goodsList.add(marketGoods);//一个个放进去
            }
            data.put("goodsList", goodsList);
        }
        //map弄进去
        data.put("topic", marketTopic);
        return BaseRespVo.ok(data);
    }

}
